package com.syw.houmao.main.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.syw.houmao.R;
import com.syw.houmao.main.bean.FirstPagerCategoryInnerBean;
import com.syw.houmao.main.bean.FirstPagerCategoryInnerDataBean;

/**
 * Created by Administrator on 2016/10/19.
 */

public class MainGridAdapter extends BaseAdapter {
    FirstPagerCategoryInnerBean mData;
    public void setData(FirstPagerCategoryInnerBean bean){
        mData = bean;
        notifyDataSetChanged();
    }
    public void swapData(FirstPagerCategoryInnerBean bean){
        mData = bean;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mData!=null?mData.getData().size():0;
    }

    @Override
    public Object getItem(int position) {
        return mData!=null?mData.getData().get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView ==null){
            convertView = LayoutInflater.from(parent.getContext().getApplicationContext()).inflate(R.layout.main_list_grid_item,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        FirstPagerCategoryInnerDataBean dataBean = mData.getData().get(position);
        holder.vViews.setText(dataBean.getViews());
        holder.vNickname.setText(dataBean.getNickname());
        holder.vChannel.setText(dataBean.getChannel());
        Picasso.with(parent.getContext().getApplicationContext()).load(dataBean.getImage()).into(holder.vImage);
        return convertView;
    }
    static class ViewHolder{
        ImageView vImage;
        TextView vNickname,vViews,vChannel;
        public ViewHolder(View view){
            vImage = (ImageView) view.findViewById(R.id.main_grid_image);
            vChannel = (TextView) view.findViewById(R.id.main_grid_channel);
            vNickname = (TextView) view.findViewById(R.id.main_grid_nickname);
            vViews = (TextView) view.findViewById(R.id.main_grid_views);
        }
    }
}
